const Merge = require("../template")

function testMergePerformance() {
  // 1. 大量对象合并性能测试
  console.log(
    "1. 大量对象合并性能测试:",
    (() => {
      const target = {}
      const sources = Array.from({ length: 100 }, (_, i) => ({
        [`key${i}`]: i,
        [`nested${i}`]: { value: i * 2 }
      }))

      const start = performance.now()
      Merge.merge(target, ...sources)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  // 2. 大量深度对象合并性能测试
  console.log(
    "2. 大量深度对象合并性能测试:",
    (() => {
      const target = { data: {} }
      const sources = Array.from({ length: 100 }, (_, i) => ({
        data: {
          [`item${i}`]: {
            id: i,
            value: i * 2,
            nested: { deep: i * 3 }
          }
        }
      }))

      const start = performance.now()
      Merge.merge(target, ...sources)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  // 3. 大量不可变合并性能测试
  console.log(
    "3. 大量不可变合并性能测试:",
    (() => {
      const target = { a: 1 }
      const sources = Array.from({ length: 100 }, (_, i) => ({
        [`key${i}`]: i
      }))

      const start = performance.now()
      Merge.mergeImmutable(target, ...sources)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  // 4. 大量自定义合并性能测试
  console.log(
    "4. 大量自定义合并性能测试:",
    (() => {
      const target = { value: 0 }
      const sources = Array.from({ length: 100 }, (_, i) => ({
        value: i
      }))
      const customizer = (objValue, srcValue) => objValue + srcValue

      const start = performance.now()
      Merge.mergeWith(target, sources[0], customizer)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  // 5. 大量数组策略合并性能测试
  console.log(
    "5. 大量数组策略合并性能测试:",
    (() => {
      const target = { items: [] }
      const sources = Array.from({ length: 100 }, (_, i) => ({
        items: [i, i + 1, i + 2]
      }))

      const start = performance.now()
      Merge.mergeWithArrayStrategy(target, ...sources, 'concat')
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  // 6. 大量条件合并性能测试
  console.log(
    "6. 大量条件合并性能测试:",
    (() => {
      const target = { a: 1, b: 2 }
      const sources = Array.from({ length: 100 }, (_, i) => ({
        [`key${i}`]: i,
        b: i + 10
      }))
      const predicate = (key, value) => key !== 'b'

      const start = performance.now()
      Merge.mergeWithCondition(target, ...sources, predicate)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  // 7. 大量类型检查合并性能测试
  console.log(
    "7. 大量类型检查合并性能测试:",
    (() => {
      const target = { a: 1, b: 'hello' }
      const sources = Array.from({ length: 100 }, (_, i) => ({
        [`key${i}`]: i,
        b: 'world'
      }))

      const start = performance.now()
      Merge.mergeWithTypeCheck(target, ...sources, true)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  // 8. 大量默认值合并性能测试
  console.log(
    "8. 大量默认值合并性能测试:",
    (() => {
      const target = { a: 1 }
      const sources = Array.from({ length: 100 }, (_, i) => ({
        [`key${i}`]: i
      }))
      const defaults = { a: 0, b: 0, c: 0 }

      const start = performance.now()
      Merge.mergeWithDefaults(target, ...sources, defaults)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  // 9. 大量路径合并性能测试
  console.log(
    "9. 大量路径合并性能测试:",
    (() => {
      const target = { user: { profile: {} } }
      const sources = Array.from({ length: 100 }, (_, i) => ({
        user: {
          profile: {
            [`field${i}`]: i
          }
        }
      }))

      const start = performance.now()
      Merge.mergeAtPath(target, ...sources, 'user.profile')
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  // 10. 大量转换合并性能测试
  console.log(
    "10. 大量转换合并性能测试:",
    (() => {
      const target = { value: 1 }
      const sources = Array.from({ length: 100 }, (_, i) => ({
        value: i
      }))
      const transformers = {
        value: (val) => val * 2
      }

      const start = performance.now()
      Merge.mergeWithTransform(target, ...sources, transformers)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  // 11. this上下文性能测试
  console.log(
    "11. this上下文性能测试:",
    (() => {
      const perfObj = {
        multiplier: 2,
        processPerformance: function(target, sources) {
          const start = performance.now()
          const merged = Merge.merge(target, ...sources)
          const end = performance.now()
          const time = end - start

          return {
            time: time,
            result: Object.keys(merged).length * this.multiplier
          }
        }
      }

      const target = { a: 1 }
      const sources = Array.from({ length: 10 }, (_, i) => ({
        [`key${i}`]: i
      }))
      const result = perfObj.processPerformance(target, sources)

      return result.time < 100 && result.result > 0
    })()
  )

  // 12. 合并性能一致性测试
  console.log(
    "12. 合并性能一致性测试:",
    (() => {
      const target = { a: 1 }
      const sources = Array.from({ length: 100 }, (_, i) => ({
        [`key${i}`]: i
      }))

      // 测试不同合并方法的一致性
      const methods = [
        () => Merge.merge(target, ...sources),
        () => Merge.mergeImmutable(target, ...sources),
        () => Merge.mergeWith(target, sources[0], (obj, src) => src),
        () => Merge.mergeWithArrayStrategy(target, ...sources, 'replace'),
        () => Merge.mergeWithCondition(target, ...sources, () => true)
      ]

      const results = methods.map(method => {
        const start = performance.now()
        const result = method()
        const end = performance.now()
        return {
          time: end - start,
          keys: Object.keys(result).length
        }
      })

      return results.every(result => result.time < 100 && result.keys > 0)
    })()
  )
}

// 执行性能测试
console.log("=== Merge 性能测试 ===")
testMergePerformance()
console.log("=== Merge 性能测试完成 ===")
